package cwinsor.com.leaf_raking;

import java.util.Arrays;
import java.util.List;

public final class TeamChange {

	/*
	 * properties
	 */
	private String name;

	/*
	 * getters
	 */
	public final String name() { return name; }

	/*
	 * private constructor
	 */
	private TeamChange(String n) { name = n; } // private constructor

	/*
	 * global instances
	 */
	public static final TeamChange ADD01 = new TeamChange("ADD01");
	public static final TeamChange ADD02 = new TeamChange("ADD02");
	public static final TeamChange ADD03 = new TeamChange("ADD03");
	public static final TeamChange ADD04 = new TeamChange("ADD04");
	public static final TeamChange ADD05 = new TeamChange("ADD05");
	public static final TeamChange ADD06 = new TeamChange("ADD06");
	public static final TeamChange ADD07 = new TeamChange("ADD07");
	public static final TeamChange ADD08 = new TeamChange("ADD08");
	public static final TeamChange ADD09 = new TeamChange("ADD09");

	public static final TeamChange ADD10 = new TeamChange("ADD10");
	public static final TeamChange ADD11 = new TeamChange("ADD11");
	public static final TeamChange ADD12 = new TeamChange("ADD12");
	public static final TeamChange ADD13 = new TeamChange("ADD13");
	public static final TeamChange ADD14 = new TeamChange("ADD14");
	public static final TeamChange ADD15 = new TeamChange("ADD15");
	public static final TeamChange ADD16 = new TeamChange("ADD16");
	public static final TeamChange ADD17 = new TeamChange("ADD17");

	public static final TeamChange DROP = new TeamChange("DROP");
	public static final TeamChange NO_CHANGE = new TeamChange("NO_CHANGE");

	/*
	 * global instances as a list (public method)
	 */
	public static final List<TeamChange> asList() {
		return instanceList;		
	}

	/*
	 * instances list (private)
	 */
	private static final List<TeamChange> instanceList = Arrays.asList(
			ADD01,
			ADD02,
			ADD03,
			ADD04,
			ADD05,
			ADD06,
			ADD07,
			ADD08,
			ADD09,
			ADD10,
			ADD11,
			ADD12,
			ADD13,
			ADD14,
			ADD15,
			ADD16,
			ADD17,
			DROP,
			NO_CHANGE);

	/*
	 * toString
	 */
	public final String toString() {
		if (this.equals(ADD01)) { return "1"; }
		if (this.equals(ADD02)) { return "2"; }
		if (this.equals(ADD03)) { return "3"; }
		if (this.equals(ADD04)) { return "4"; }
		if (this.equals(ADD05)) { return "5"; }
		if (this.equals(ADD06)) { return "6"; }
		if (this.equals(ADD07)) { return "7"; }
		if (this.equals(ADD08)) { return "8"; }
		if (this.equals(ADD09)) { return "9"; }
		if (this.equals(ADD10)) { return "10"; }
		if (this.equals(ADD11)) { return "11"; }
		if (this.equals(ADD12)) { return "12"; }
		if (this.equals(ADD13)) { return "13"; }
		if (this.equals(ADD14)) { return "14"; }
		if (this.equals(ADD15)) { return "15"; }
		if (this.equals(ADD16)) { return "16"; }
		if (this.equals(ADD17)) { return "17"; }
		if (this.equals(DROP)) { return "D"; }
		if (this.equals(NO_CHANGE)) { return " "; }
		Msg.fatal(dS + ": when performing toString - hit the default case");
		return null;
	}

	/*
	 * take in an Object and convert it to a TeamChange
	 * this is mostly used when taking in raw user input
	 * so cast to string and checking is required
	 */
	public static TeamChange objectToTeamChange(Object value) {

		// cast object to string
		String inStringRaw = value.toString();

		// Msg.note("TeamChange: inStringRaw was \"" + inStringRaw + "\"");

		if (inStringRaw == null) {
			Msg.note("TeamChange: inStringRaw is null - it is ignored");
			return NO_CHANGE;
		}

		if (inStringRaw.length() == 0) {
			Msg.note("TeamChange: inStringRaw has zero length - it is ignored");
			return NO_CHANGE;
		}

		else if (inStringRaw.substring(inStringRaw.length()-1, inStringRaw.length()).equals(" ")) {
			return NO_CHANGE;
		}

		else if (inStringRaw.substring(inStringRaw.length()-1, inStringRaw.length()).equals("d")) {
			return DROP;
		}

		else {
			String tensChar;
			String onesChar;

			if (inStringRaw.length() == 1) {
				tensChar = "0";
			} else {
				tensChar = inStringRaw.substring(inStringRaw.length()-2, inStringRaw.length()-1);
			}
			onesChar = inStringRaw.substring(inStringRaw.length()-1, inStringRaw.length());

			String inStringTrimmed = new String(tensChar + onesChar);

			if (inStringTrimmed.equals("01")) { return ADD01; }
			if (inStringTrimmed.equals("02")) { return ADD02; }
			if (inStringTrimmed.equals("03")) { return ADD03; }
			if (inStringTrimmed.equals("04")) { return ADD04; }
			if (inStringTrimmed.equals("05")) { return ADD05; }
			if (inStringTrimmed.equals("06")) { return ADD06; }
			if (inStringTrimmed.equals("07")) { return ADD07; }
			if (inStringTrimmed.equals("08")) { return ADD08; }
			if (inStringTrimmed.equals("09")) { return ADD09; }

			if (inStringTrimmed.equals("10")) { return ADD10; }
			if (inStringTrimmed.equals("11")) { return ADD11; }
			if (inStringTrimmed.equals("12")) { return ADD12; }
			if (inStringTrimmed.equals("13")) { return ADD13; }
			if (inStringTrimmed.equals("14")) { return ADD14; }
			if (inStringTrimmed.equals("15")) { return ADD15; }
			if (inStringTrimmed.equals("16")) { return ADD16; }
			if (inStringTrimmed.equals("17")) { return ADD17; }

			// default - print message and return NO_CHANGE
			Msg.note("TeamChange: hit the default case with \"" + inStringRaw + "\" it is ignored");
		}
		return NO_CHANGE;
	}


	// debug
	private String dS = getClass().getSimpleName();
}
